package com.weishu.controller;

import com.alibaba.fastjson.JSON;

import com.gaofei.service.FileService;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.weishu.domain.PremisesInfoEntity;
import com.weishu.domain.Reigion;
import com.weishu.domain.Shangquan;
import com.weishu.domain.SubWay;
import com.weishu.service.LoupanService;
import com.weishu.service.SanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author huizhenfei
 * @Description //TODO qq231
 * @Date 20:53 2021/5/18
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/weishu")
public class LoupanController {
    @Autowired
    private LoupanService loupanService;

    @Autowired
    RedisTemplate redisTemplate;


    @RequestMapping("getloupanlist")
    public AjaxResult getloupanlist(@RequestBody Map<String,Object>map){
        String pageNum = map.get("pageNum").toString();
        String pageSize = map.get("pageSize").toString();
        List<PremisesInfoEntity> list=loupanService.getloupanlist(map,Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        Object o = JSON.toJSON(list);
        redisTemplate.opsForSet().add("list",o);
        PageInfo<PremisesInfoEntity> tt=new PageInfo<PremisesInfoEntity>(list);
        return AjaxResult.success(tt);
    }

    @Autowired
    private SanService sanService;

    @RequestMapping("getshenglist")
    public List<Reigion> getsheng(@RequestBody Map<String,Object>map){
        String region_ID = map.get("region_ID").toString();
        System.out.println("controller层里面的东西"+region_ID);
        List<Reigion> slist=sanService.getSlist(Integer.parseInt(region_ID));
        return slist;
    }


    @RequestMapping("addloupan")
    public boolean addone(@RequestBody PremisesInfoEntity premisesInfoEntity){
        System.out.println(premisesInfoEntity);
        //商圈处理
        List<Integer> sqa = premisesInfoEntity.getSqa();
        String str="";
        for (Integer integer : sqa) {
            str+=integer+",";
        }
        String st1 = str.substring(0, str.length() - 1);
        premisesInfoEntity.setCommercialArea(st1);
        //地铁处理
        List<Integer> dt = premisesInfoEntity.getDt();
        String str2="";
        for (Integer integer : dt) {
            str2+=integer+",";
        }
        String st2 = str2.substring(0, str2.length() - 1);
        premisesInfoEntity.setSubway(st2);
        //建筑类型
        List<String> jianzhu = premisesInfoEntity.getJianzhu();
        String str3="";
        for (String s : jianzhu) {
            str3+=s+",";
        }
        String st3 = str3.substring(0, str3.length() - 1);
        premisesInfoEntity.setBuildingType(st3);
        //社区类型
        Integer she = premisesInfoEntity.getShe();
        premisesInfoEntity.setCommunityName(she.toString());
        //物业类型
        List<String> wytype = premisesInfoEntity.getWytype();
        String str4="";
        for (String s : wytype) {
            str4+=s+",";
        }
        String st4 = str4.substring(0, str4.length() - 1);
        premisesInfoEntity.setPremises_type(st4);
        //楼盘特色
        List<String> tese = premisesInfoEntity.getTese();
        String str5="";
        for (String s : tese) {
            str5+=s+",";
        }
        String st5 = str5.substring(0, str5.length() - 1);
        //propertyFeatures
        premisesInfoEntity.setPropertyFeatures(st5);

        System.out.println(premisesInfoEntity+"修改后的实体类看值是啥");
        int ktt=loupanService.addone(premisesInfoEntity);

        if (ktt>0){
            return true;
        }
        return false;
    }
    @Autowired
    private FileService fileService;

    @RequestMapping("/uploadFile")
    public static String upload(MultipartFile file) {
        // 获取原始名称
        String originalFilename = file.getOriginalFilename();
        // 确保名字唯一
        String fileName = System.currentTimeMillis() + originalFilename;
        // 每天一个文件夹
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        String path = "d://pic//" + format;
        File file3 = new File(path);
        // 如果不存在就创建
        if (!file3.exists()) {
            file3.mkdirs();
        }
        try {
            file.transferTo(new File(path , fileName));
        } catch (IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("pic/" + format + "/" + fileName);
        return "D:/pic/" + format + "/" + fileName;
    }

    //测试
    @RequestMapping("ceshi")
    public String tice(@RequestParam("ceshi")String ceshizhi){
        System.out.println(ceshizhi);
        return   "ok";

    }
    //获取所有的地铁列表
    @RequestMapping("getsubwaylist")
    public List<SubWay> getsubwaylist(@RequestBody Map<String,Object>map){
        String region_ID = map.get("region_ID").toString();
        System.out.println("controller层里面的东西"+region_ID);
        List<SubWay> slist=sanService.getsubwaylist(Integer.parseInt(region_ID));
        return slist;
    }
    //获取所有的商圈
    @RequestMapping("getsqlist")
    public List<Shangquan> getshangquan(@RequestBody Map<String,Object>map){
        String code = map.get("code").toString();
        List<Shangquan> list=sanService.getShangquan(code);
        return list;
    }


}
